package hit.util;

import gui.item.ItemData;
import gui.product.ProductData;
import hit.core.Item;
import hit.core.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * Provides methods for translating our hit core objects to the GUI's data objects
 * */
public class DataConverter {
	
	/**
	 * Convert a {@link Product} into a {@link ProductData}
	 * @param product the Product to convert
	 * @return the converted ProductData
	 * */
	public static ProductData TranslateProduct(Product product) {
		
		ProductData result = new ProductData();
		
		result.setBarcode(product.GetBarcode().GetValue());
		result.setCount("0");
		result.setDescription(product.GetDescription());
		result.setShelfLife(Integer.toString(product.GetShelfLife()));
		result.setSize(product.GetSize().toString());
		result.setSupply(Integer.toString(product.GetThreeMonth()));
		result.setTag(product);
		
		return result;
	}
	
	public static List<ProductData> TranslateProducts(List<Product> products) {
		
		List<ProductData> result = new ArrayList<ProductData>();
		
		for (int i = 0; i < products.size(); i++) {
			result.add(TranslateProduct(products.get(i)));
		}
		
		return result;
	}
	
	/**
	 * Convert an {@link Item} into an {@link ItemData}
	 * @param item the Item to convert
	 * @return the converted Item
	 * */
	public static ItemData TranslateItem(Item item) {
		
		ItemData result = new ItemData();
		
		result.setBarcode(item.GetBarcode().GetValue());
		result.setEntryDate(item.GetEntry());
		result.setExpirationDate(item.GetExpiration());
		result.setStorageUnit(item.GetRootStorageUnit().GetName());
		if (item.GetContainer().equals(item.GetRootStorageUnit())){
			result.setProductGroup("");
		}
		else{
			result.setProductGroup(item.GetContainer().GetName());
		}
		result.setTag(item);
		
		return result;
	}
	
	/**
	 * Convert a List of {@link Item}s into a List of {@link ItemData}
	 * @param items the List of Items to convert
	 * @return the converted List of ItemData
	 * */
	public static List<ItemData> TranslateItems(List<Item> items) {
		
		List<ItemData> result = new ArrayList<ItemData>();
		
		for (int i = 0; i < items.size(); i++) {
			result.add(TranslateItem(items.get(i)));
		}
		
		return result;
	}
}
